<?php
/**
 * Created by PhpStorm.
 * User: poptser@foxmail.com
 * Date: 2018/1/30
 * Time: 14:04
 */
namespace App\Http\Controllers\WarehouseManage;
use App\Http\Controllers\ApiController;
use App\Services\Sorting\SortingService;
use App\Services\SortingWeb\ToolService;
use App\Services\Warehouse\InventoryOperationService;
use App\Services\Warehouse\StockCountSchemeService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

//盘点管理控制器
class PandianController extends ApiController{

    public $userId;
    public $wid;
    public $toolService;
    public $widInfo;
    public $kindeeSn;
    public $sortingService;
    protected $countSchemeService;   //盘点相关
    protected $InventoryOperationService;

    public function __construct(ToolService $toolService,StockCountSchemeService $countSchemeService,
                                SortingService $sortingService,InventoryOperationService $inventoryOperationService){


        $isLogion = Auth::guard('sorting')->check();
        if(!$isLogion){
            $this->returnErr("api_token 不存在或已过期");
        }else{
            $this->userId = Auth::guard('sorting')->id();
            if($this->checkUserType(3,$this->userId)){
                $this->returnErr("您没有访问此接口得权限");
            }else{
                $this->toolService = $toolService;
                $this->wid = $this->toolService->getUserWid($this->userId);
                $this->widInfo = $this->toolService->getWidInfo($this->wid);
                $this->kindeeSn = $this->widInfo->k3_number;
                $this->countSchemeService=$countSchemeService;    //库存相关
                $this->sortingService=$sortingService;
                $this->InventoryOperationService=$inventoryOperationService;
            }
        }
    }

    /**
     * 获取商品详情
     * Author:poptser@foxmail.com
     * @param Request $request
     * @return mixed
     */
    public function getGoodsInfo(Request $request){
        $goodSn = $request->goodSn;
        if(empty($goodSn)){
            return $this->failedReturn("goodSn 为空");
        }
        $goodsInfo = DB::table("goods")->where("goods_sn",$goodSn)
            ->select(['goods_img','goods_name','goods_sn','supplier_code as k3_supplier_sn','consignment as k3_vmi'])
            ->first();
        if(empty($goodsInfo)){
            return $this->failedReturn("未找到此商品");
        }

        //创建盘点单
        $setPandian = $this->setPandianOrder($goodSn);

        if(!is_array($setPandian)){
            return $this->failedReturn($setPandian);
        }

        try{
            $checkOrderInfo=$this->countSchemeService->SubmitAndAuditStockCountSchemeAndIO($setPandian['Number']);
            if(!$checkOrderInfo){
                return $this->failedReturn('盘点单读取失败');
            }
        }catch (\Exception $exception){
            return $this->failedReturn($exception->getMessage());
        }
        $checkOrderList = [];

        foreach ($checkOrderInfo as $oneList){

            $goodsShelf =  $this->sortingService->getGoodsShelf($goodSn,$this->wid,true);

            $checkOrder = [
                "goodSn"=>$oneList['FMaterialId.FNumber'],      //货号
                "goodsName"=>$oneList['FMaterialId.FName'],     //商品名称
                "PdNumber"=>$oneList['FBillNo'],                //盘点单号
                "PdId"=>$oneList['FID'],                        //盘点单ID
                "wName"=>$oneList['FStockId.FName'],            //仓库名称
                "GoodsNum"=>$oneList['FAcctQty'],               //商品当前库存
                "supplierName"=>$oneList['FOwnerId.FName'],     //货主名称
                "PdGoodsId"=>$oneList['FBillEntry_FEntryID'],   //盘点作业单内码
                "goodsShelf" => [$goodsShelf['shelf_info']]
            ];
            $checkOrderList[] = $checkOrder;
        }
        return $this->success($checkOrderList);
    }

    /**
     * 创建盘点单
     * Author:poptser@foxmail.com
     * @param $goodSn
     * @return array|bool|mixed|string
     */
    public function setPandianOrder($goodSn){
        $data=[
            'wid'=>$this->wid,
            'goods_sn'=>$goodSn,
            'note'=>"App 创建盘点单"
        ];

        try{
            $return=$this->countSchemeService->CreateStockCountScheme($data);
            if($return){
                return $return;
            }else{
                return '盘点单创建失败';
            }
        }catch (\Exception $exception){
            return $exception->getMessage();
        }
    }


    /**
     * 提交盘点数
     * Author:poptser@foxmail.com
     * @param Request $request
     * @return mixed
     */
    public function setGoodsNum(Request $request){

        $goodsList = json_decode($request->goodList,true);

        $data=[
            'FID'=>$request->PdId,
            'FBillNo'=>$request->PdNumber,
            'note'=>"App:".$request->remark,
            'goodsdata'=>[
                [
                    "fentryid"=>$goodsList[0]['PdGoodsId'],
                    "fcountqty"=>$goodsList[0]['goodsNum']
                ]
            ]
        ];
        try{
            $return=$this->InventoryOperationService->UpdateInventoryOperationAndSubmitAndAudit($data);
            if($return){
                return $this->success($return);
            }else{
                return $this->failed('操作失败');
            }
        }catch (\Exception $exception){
            return $this->failedReturn($exception->getMessage());
        }
    }
}